package com.kingzoon.seckill.web;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kingzoon.seckill.dto.Exposer;
import com.kingzoon.seckill.dto.SeckillExecution;
import com.kingzoon.seckill.dto.SeckillResult;
import com.kingzoon.seckill.entity.Seckill;
import com.kingzoon.seckill.enums.MacroDefine;
import com.kingzoon.seckill.exception.SeckillException;
import com.kingzoon.seckill.service.SeckillService;

@Controller
@Api(value="产品秒杀")
@RequestMapping("/seckill")
public class SeckillController {
	
	@Autowired
	private SeckillService seckillService;
	
	@ApiOperation(value="产品秒杀列表")
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String list(Model model){
		List<Seckill> list = seckillService.getSeckillList();
		model.addAttribute("list", list);
		return "list";
	}
	
	@ApiOperation(value="产品列表")
	@RequestMapping(value="/product",method=RequestMethod.GET,produces={"application/json;charet=utf-8"})
	@ResponseBody
	public SeckillResult<List<Seckill>> product(){
		List<Seckill> list = seckillService.getSeckillList();
		SeckillResult<List<Seckill>> result = new SeckillResult<List<Seckill>>(true, list);
		return result;
	}
	
	@ApiOperation(value="产品秒杀明细")
	@RequestMapping(value="/{seckillId}/detail",method=RequestMethod.GET)
	public String detail(@PathVariable("seckillId")long seckillId,Model model){
		Seckill seckill = seckillService.getById(seckillId);
		model.addAttribute("seckill", seckill);
		return "detail";
	}
	
	@ApiOperation(value="获取秒杀暴露地址")
	@RequestMapping(value="/{seckillId}/exposer",method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	@ResponseBody
	public SeckillResult<Exposer> exposer(@PathVariable("seckillId")long seckillId){
		Exposer exposer = seckillService.exportSeckillUrl(seckillId);
		SeckillResult<Exposer> result = new SeckillResult<Exposer>(true, exposer);
		return result;	
	}
	
	@ApiOperation(value="执行秒杀")
	@RequestMapping(value = "/{seckillId}/{md5}/execution",
            method=RequestMethod.POST,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public SeckillResult<SeckillExecution> execute(@PathVariable("seckillId") Long seckillId,@PathVariable("md5") String md5,
                                                   @CookieValue(value = "killPhone",required = false) Long phone){
        //springmvc valid
        if(phone==null){
            return new SeckillResult<SeckillExecution>(false,"未注册");
        }
        try {
            //调用存储过程去执行
            //SeckillExecution execution=seckillService.executeSeckillProcedure(seckillId,phone,md5);
            SeckillExecution execution=seckillService.executeSeckill(seckillId,phone,md5);
            return new SeckillResult<SeckillExecution>(true,execution);
        }catch (SeckillException e) {
            SeckillExecution execution=new SeckillExecution(seckillId,MacroDefine.SECKILLSTATE.INNER_ERROE.getState());
            return new SeckillResult<SeckillExecution>(true,execution);
        }
    }
	
	@ApiOperation(value="获取系统时间")
    @RequestMapping(value = "/time/now",method = RequestMethod.GET)
    @ResponseBody
    public SeckillResult<Long> time(){
        Date now=new Date();
        return new SeckillResult<Long>(true,now.getTime());
    }

}
